package com.jiaquyun.jcyx.main.mine

import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.entity.Sign
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_integral.*
import java.lang.StringBuilder
import java.text.SimpleDateFormat
import java.util.*

class MineIntegralActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MineIntegralActivity::class.java)
        }
    }

    private val mSignListAdapter = SignListAdapter()

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_integral

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.white, view = flMineIntegralBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { onBackPressed() }

        rvMineIntegralBody.adapter = mSignListAdapter.apply {
            setNewInstance(mWeekData)
        }

        tvMineIntegralSign.setOnClickListener {
            viewModel.userScoreSign()
        }
        tvMineIntegralDetails.setOnClickListener {
            IntegralDetailsActivity.open()
        }
        tvMineIntegralSign.isEnabled = false
        tvMineIntegralPrice.text = "${JCApplication.userIndex?.userInfo?.score ?: "0"}"
    }

    override fun initData() {
        viewModel.userScoreSignList()
    }

    override fun initViewMode() {
        viewModel.userSignListLiveDate.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }

            val data = response.data?.data
                ?: return@observe


            val sign = data.sign
            val newSign: MutableList<Sign> = mutableListOf()
            val weekData: MutableList<Sign> = mWeekData
            for (item in weekData) {
                val value = sign.find {
                    it.createtime_text?.contains(item.createtime_text ?: "") ?: false
                }
                if (value != null) {
                    newSign.add(value.apply { this.name = item.name })
                } else {
                    newSign.add(item)
                }
            }
            mSignListAdapter.setNewInstance(newSign)

            tvMineIntegralSign.apply {
                if (sign.find {
                        it.createtime_text?.contains(
                            TimeUtils.millis2String(
                                System.currentTimeMillis(),
                                "yyyy-MM-dd"
                            )
                        ) == true
                    } == null) {
                    text = StringBuilder().append("签到(今日可领取")
                        .append(data.score_reward_day).append("积分)")
                    isEnabled = true
                } else {
                    text = "今日已签到"
                    isEnabled = false
                }
            }

            val rule = StringBuilder().append("签到规则")
            data.score_rule_text.forEach {
                rule.append("\n").append(it)
            }
            tvMineIntegralRule.text = rule.toString()
        })

        viewModel.userSignLiveDate.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.msg)
            initData()
        })
    }

    //获取当天星期几
    val mWeekData: MutableList<Sign>
        get() {
            val data: MutableList<Sign> = ArrayList()
            val format = SimpleDateFormat("yyyy-MM-dd")
            val c = Calendar.getInstance()
            //获取当天星期几
            val mWay = c[Calendar.DAY_OF_WEEK]
            //将时间退到周日
            for (i in 0 until mWay) {
                c.add(Calendar.DATE, -7)
            }
            //得到本周时间
            for (i in 0..6) {
                c.add(Calendar.DATE, 1)
                data.add(
                    Sign().apply {
                        this.createtime_text = format.format(c.time)
                        this.name = getWeekDay(c[Calendar.DAY_OF_WEEK], mWay)
                    }
                )
            }

            return data
        }

    private fun getWeekDay(day: Int, today: Int): String? {
        var s: String? = null
        when (day) {
            2 -> s = "周一"
            3 -> s = "周二"
            4 -> s = "周三"
            5 -> s = "周四"
            6 -> s = "周五"
            7 -> s = "周六"
            1 -> s = "周日"
        }
        if (day == today) s = "今日"
        return s
    }

}